using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Zhaoxi.MSACommerce.CategoryService.Infrastructure.Data.Configuration;
using Zhaoxi.MSACommerce.Infrastructure._Common;
using ZhaoXi.MSACommerce.Infrastructure.EnityFrameworkCore;
using ZhaoXi.MSACommerce.Infrastructure.EnityFrameworkCore.interceptor;

namespace Zhaoxi.MSACommerce.CategoryService.Infrastructure;

public static class DependencyInjection
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        ConfigureEfCore(services, configuration);
            
        services.AddInfrastructureCommon();
            
        services.AddInfrastructurea(configuration); //先去掉服务发现
        return services;
    }

    private static void ConfigureEfCore(IServiceCollection services, IConfiguration configuration)
    {
        var dbConnection = configuration.GetConnectionString("DefaultConnection");

        services.AddDbContext<CategoryDbContext>((sp, options) =>
        {
            options.AddInterceptors(sp.GetRequiredService<AuditEntityInterceptor>());
            options.UseMySql(dbConnection, new MySqlServerVersion(new Version(8, 0, 0)));
        });
    }
}